package core.view
{
	import core.model.PSharedObject;
	
	import flash.events.FocusEvent;
	import flash.events.MouseEvent;
	import flash.net.URLRequest;
	import flash.net.navigateToURL;
	import flash.system.System;
	import flash.text.TextFieldType;
	
	import org.ais.event.TEvent;
	import org.aisy.button.Button;
	import org.aisy.display.USprite;

	public class PlayListItem extends USprite
	{
		protected var NAME:String;
		protected var index:uint;
		
		protected var _itemBg:Button;
		
		public function PlayListItem(name:String, index:uint, data:*)
		{
			NAME = name;
			this.index = index;
			init();
			name = null;
			data = null;
		}
		
		protected function init():void
		{
			_itemBg = new Button();
			_itemBg.setClass(SWF_PLAYLIST_ITEM);
			_itemBg.getSkin().txt_title.text = PSharedObject.getInstance().data.p_list[index][1];
			if (index !== PSharedObject.getInstance().data.p_int) {
				_itemBg.getSkin().mc_icon.visible = false;
			}
			__adjustTitle();
			addChild(_itemBg);
			
			__addEvent();
		}
		
		protected function __addEvent():void
		{
			_itemBg.getSkin().txt_title.addEventListener(FocusEvent.FOCUS_IN, __txtFocHandler);
			
			_itemBg.addEventListener(MouseEvent.DOUBLE_CLICK, __mouseHandler);
			_itemBg.setMouseDown(__mouseHandler);
			_itemBg.setClick(__mouseHandler);
			
			TEvent.newTrigger(NAME + "ITEM", __triggerHandler);
		}
		
		protected function __removeEvent():void
		{
			_itemBg.getSkin().txt_title.removeEventListener(FocusEvent.FOCUS_IN, __txtFocHandler);
			_itemBg.getSkin().txt_title.removeEventListener(FocusEvent.FOCUS_OUT, __txtFocHandler);
		}
		
		protected function __adjustTitle():void
		{
			if (_itemBg.getSkin().txt_title.type === TextFieldType.INPUT) {
				_itemBg.getSkin().txt_title.text = PSharedObject.getInstance().data.p_list[index][1];
			}
			else {
				if (_itemBg.getSkin().txt_title.textWidth + 2 > _itemBg.getSkin().txt_title.width) {
					var i:uint = _itemBg.getSkin().txt_title.length;
					while (i > 0) {
						i--;
						_itemBg.getSkin().txt_title.text = _itemBg.getSkin().txt_title.text.slice(0, i);
						_itemBg.getSkin().txt_title.appendText("...");
						if (_itemBg.getSkin().txt_title.textWidth + 2 < _itemBg.getSkin().txt_title.width) {
							return;
						}
					}
				}
			}
		}
		
		protected function __txtFocHandler(e:FocusEvent):void
		{
			switch (e.type) {
				case FocusEvent.FOCUS_IN:
					_itemBg.getSkin().txt_title.removeEventListener(FocusEvent.FOCUS_IN, __txtFocHandler);
					_itemBg.getSkin().txt_title.addEventListener(FocusEvent.FOCUS_OUT, __txtFocHandler);
					
					_itemBg.getSkin().txt_title.selectable = true;
					_itemBg.getSkin().txt_title.type = TextFieldType.INPUT;
					break;
				case FocusEvent.FOCUS_OUT:
					_itemBg.getSkin().txt_title.removeEventListener(FocusEvent.FOCUS_OUT, __txtFocHandler);
					_itemBg.getSkin().txt_title.addEventListener(FocusEvent.FOCUS_IN, __txtFocHandler);
					
					_itemBg.getSkin().txt_title.selectable = false;
					_itemBg.getSkin().txt_title.type = TextFieldType.DYNAMIC;
					
					if (e.shiftKey) {
						PSharedObject.getInstance().data.p_list[index][0] = _itemBg.getSkin().txt_title.text;
						PSharedObject.getInstance().flush();
						_itemBg.getSkin().txt_title.text = PSharedObject.getInstance().data.p_list[index][1];
					}
					else if (_itemBg.getSkin().txt_title.text !== PSharedObject.getInstance().data.p_list[index][1]) {
						PSharedObject.getInstance().data.p_list[index][1] = _itemBg.getSkin().txt_title.text;
						PSharedObject.getInstance().flush();
						TEvent.trigger(NAME, "CHANGE_PLAY_NAME", _itemBg.getSkin().txt_title.text);
					}
					break;
			}
			__adjustTitle();
			e = null;
		}
		
		protected function __mouseHandler(e:MouseEvent):void
		{
			switch (e.type) {
				case MouseEvent.MOUSE_DOWN:
					if (e.ctrlKey && e.altKey) {
						navigateToURL(new URLRequest(PSharedObject.getInstance().data.p_list[index][0]));
					}
					else if (e.ctrlKey) {
						System.setClipboard(PSharedObject.getInstance().data.p_list[index][0]);
					}
					else if (e.altKey) {
						System.setClipboard(PSharedObject.getInstance().data.p_list[index]);
					}
					break;
				case MouseEvent.CLICK:
					switch (e.target.name) {
						case "btn_del":
							TEvent.trigger(NAME, "DELETE", index);
							break;
					}
					break;
				case MouseEvent.DOUBLE_CLICK:
					TEvent.trigger(NAME, "PLAY", index);
					TEvent.trigger(NAME + "ITEM", "PLAY", index);
					break;
			}
			e = null;
		}
		
		protected function __triggerHandler(type:String, data:* = null):void
		{
			switch (type) {
				case "PLAY":
					if (index !== data) {
						_itemBg.getSkin().mc_icon.visible = false;
					}
					else {
						_itemBg.getSkin().mc_icon.visible = true;
					}
					break;
			}
			type = null;
			data = null;
		}
		
		override public function clear():void
		{
			__removeEvent();
			_itemBg = null;
			NAME = null;
			super.clear();
		}
		
	}
}